#include"IRremote.h"  //预加载红外遥控库
#include "U8glib.h"   //预加载OLED显示屏控制库
#include"Beep_tone.h" //预加载音乐库
U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9);   //D0=13  D1=11 RES=5V DC=9 CS=10
int RECV_PIN = 3;     //红外接收器所在引脚为3
IRrecv irrecv(RECV_PIN);  
decode_results results;  //储存编码结果
int Internaltime;
long IR;

void setup() {
  pinMode(speakerPin, OUTPUT);  
  irrecv.enableIRIn();        //初始化红外解码
  u8g.firstPage(); 
  do
  {
    start();                        //运行start函数
  } while (u8g.nextPage());
  delay(500);
}

void start() {
  u8g.setFont(u8g_font_gdr11r);          // 设定字体
  u8g.drawStr(30, 30, "Welcome");        //定位、输入
  u8g.setFont(u8g_font_tpss);            //设定字体
  u8g.drawStr(20, 50, "Timingprogram V1.2");
}

void work() {
  u8g.setFont(u8g_font_profont22);
  u8g.drawStr(0, 30, "Set the time");
  u8g.setFont(u8g_font_tpssb);
  u8g.setPrintPos(40, 55);
  u8g.print(Internaltime);
  u8g.drawStr(50, 55, "min");
}

void work2() {
  u8g.setFont(u8g_font_fur11);
  u8g.drawStr(20, 30, "Set the time");
  u8g.setFont(u8g_font_tpssb);
  u8g.setPrintPos(40, 55);
  u8g.print(Internaltime);
  u8g.drawStr(90, 55, "S");
}

void song() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }
  }
}
void loop() {
  if (irrecv.decode(&results)) {         //如果接收到红外信号
    IR = results.value;                  
    if (IR == 16761405) {                //如果接收到确定指令
      Internaltime = Internaltime * 60;  //把分钟单位换成秒
      for (; Internaltime > 0; Internaltime--) {
        u8g.firstPage();
        do
        {
          work2();                        //运行work2函数
        } while (u8g.nextPage());
        delay(1000);
      }
      irrecv.resume();                     //接收下一个红外指令
      for(int i=2;i>0;i--){                //重复提醒音乐2次
       song();
      }
    }
    else {
      IR = results.value;
      switch (IR) {
        case 16724175:   //接收到1
          Internaltime = Internaltime + 1; //内部时间，分钟为单位
          break;
        case 16718055:  //接受到2
          Internaltime = Internaltime + 2;
          break;
        case 16743045:
          Internaltime = Internaltime + 3;
          break;
        case 16716015:
          Internaltime = Internaltime + 4;
          break;
        case 16726215:
          Internaltime = Internaltime + 5;
          break;
        case 16734885:
          Internaltime = Internaltime + 6;
          break;
        case 16728765:
          Internaltime = Internaltime + 7;
          break;
        case 16730805:
          Internaltime = Internaltime + 8;
          break;
        case 16732845:
          Internaltime = Internaltime + 9;
          break;
        case 16748655:
          Internaltime = 0;
          break;
      }

      u8g.firstPage();
      do
      {
        work();                        //运行work函数
      } while (u8g.nextPage());
      irrecv.resume();
    }
  }
}

